package com.ss.android.article.base.utils.a;

import android.content.SharedPreferences;
import android.text.TextUtils;

import com.bytedance.common.utility.c.SharedPreferenceCompat;
import com.ss.android.newmedia.BaseApplication;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public final class b {
    public static HashMap mPrefixMap;
    private static Boolean isSpMigrated = null;
    private static String c;
    private static String d;

    static {
        HashMap map = new HashMap();
        mPrefixMap = map;
        map.put("category", "cat@");
        mPrefixMap.put("hotword", "hot@");
        mPrefixMap.put("promotion_config", "con@");
        mPrefixMap.put("sp_update_msg", "msg@");
        mPrefixMap.put("sp_update_notification", "not@");
        c = "shared_prefs";
        d = ".xml";
    }

    public static void migrateSp() {
        String v4_1;
        Object v5_1;
        Object v3_2;
        String v3_1 = null;
        Map v11;
        SharedPreferences v10;
        int v2 = 1;
        if (!isSpMigrated()) {
            if (BaseApplication.getApplication().getSharedPreferences("main_app_settings", 0).getInt("main@sp_migrate_count", 0) > 3) {
                v2 = 0;
            }

            if (v2 != 0) {
                Iterator<String> iterator = mPrefixMap.keySet().iterator();
                SharedPreferences sharedPreferences = BaseApplication.getApplication().getSharedPreferences("main_app_settings", 0);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                try {
                    Object key;
                    do {
                        if (!iterator.hasNext()) {
                            break;
                        }

                        key = iterator.next();
                        if (TextUtils.isEmpty(((CharSequence) key))) {
                            continue;
                        }

                        v10 = BaseApplication.getApplication().getSharedPreferences(((String) key), 0);
                        v11 = v10.getAll();
                        if (v11 == null) {
                            continue;
                        }

                        if (v11.size() > 0) {
                            Iterator v12 = v11.keySet().iterator();
                            while (true) {
                                if (!v12.hasNext()) {
                                    break;
                                }

                                v3_2 = v12.next();
                                v5_1 = v11.get(v3_2);
                                if (!TextUtils.isEmpty((CharSequence) mPrefixMap.get(key))) {
                                    v4_1 = mPrefixMap.get(key) + (((String) v3_2));
                                } else {
                                    v4_1 = (String) v3_2;
                                }

                                if ((v5_1 instanceof Integer)) {
                                    editor.putInt(v4_1, ((Integer) v5_1).intValue());
                                    continue;
                                }

                                if ((v5_1 instanceof Boolean)) {
                                    editor.putBoolean(v4_1, ((Boolean) v5_1).booleanValue());
                                    continue;
                                }

                                if ((v5_1 instanceof Float)) {
                                    editor.putFloat(v4_1, ((Float) v5_1).floatValue());
                                    continue;
                                }

                                if ((v5_1 instanceof Long)) {
                                    editor.putLong(v4_1, ((Long) v5_1).longValue());
                                    continue;
                                }

                                if (!(v5_1 instanceof String)) {
                                    continue;
                                }

                                editor.putString(v4_1, ((String) v5_1));
                                continue;
                            }
                            SharedPreferenceCompat.commit(v10.edit().clear());
                            continue;
                        }
                    } while (true);

                    isSpMigrated = Boolean.valueOf(true);
                    Iterator v4 = mPrefixMap.keySet().iterator();
                    while (v4.hasNext()) {
                        key = v4.next();
                        if (TextUtils.isEmpty(((CharSequence) key))) {
                            continue;
                        }
                        if (BaseApplication.getApplication().getFilesDir() != null && BaseApplication.getApplication().getFilesDir().getParentFile() != null) {
                            v3_1 = BaseApplication.getApplication().getFilesDir().getParentFile().getAbsolutePath();
                        }
                        if (TextUtils.isEmpty(((CharSequence) v3_1))) {
                            continue;
                        }
                        File v5 = new File(v3_1 + "/" + c + "/" + (((String) key)) + d);
                        if (!v5.exists()) {
                            continue;
                        }
                        v5.delete();
                    }
                    editor.putBoolean("main@is_sp_migrated", true);
                    SharedPreferenceCompat.commit(editor);
                } catch (Exception e) {
                    isSpMigrated = Boolean.valueOf(false);
                    editor.putInt("main@sp_migrate_count", sharedPreferences.getInt("main@sp_migrate_count", 0) + 1);
                    SharedPreferenceCompat.commit(editor);
                }
            }
        }
    }

    public static boolean isSpMigrated() {
        if (isSpMigrated == null) {
            isSpMigrated = Boolean.valueOf(BaseApplication.getApplication().getSharedPreferences("main_app_settings", 0)
                    .getBoolean("main@is_sp_migrated", false));
        }
        return isSpMigrated.booleanValue();
    }
}

